//
//  EaseBubbleView+Leave.m
//  ChatDemo-UI2.0
//
//  Created by jiuzhou on 16/3/17.
//  Copyright © 2016年 jiuzhou. All rights reserved.
//

#import "EaseBubbleView+Leave.h"

@implementation EaseBubbleView (Leave)
#pragma mark - private

- (void)_setupLeaveBubbleMarginConstraints
{
    NSLayoutConstraint *marginTopConstraint = [NSLayoutConstraint constraintWithItem:self.LeaveBackView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeTop multiplier:1.0 constant:self.margin.top];
    NSLayoutConstraint *marginBottomConstraint = [NSLayoutConstraint constraintWithItem:self.LeaveBackView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-self.margin.bottom];
    NSLayoutConstraint *marginLeftConstraint = [NSLayoutConstraint constraintWithItem:self.LeaveBackView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-self.margin.right];
    NSLayoutConstraint *marginRightConstraint = [NSLayoutConstraint constraintWithItem:self.LeaveBackView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:self.margin.left];
    
    [self.marginConstraints removeAllObjects];
    [self.marginConstraints addObject:marginTopConstraint];
    [self.marginConstraints addObject:marginBottomConstraint];
    [self.marginConstraints addObject:marginLeftConstraint];
    [self.marginConstraints addObject:marginRightConstraint];
    
    [self addConstraints:self.marginConstraints];
}

- (void)_setupLeaveBubbleConstraints
{
    [self _setupLeaveBubbleMarginConstraints];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgType attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgType attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:21]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstLineView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.msgType attribute:NSLayoutAttributeBottom multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstLineView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:1]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lbStart attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.firstLineView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lbStart attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:18]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lbEnd attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.lbStart attribute:NSLayoutAttributeBottom multiplier:1.0 constant:4]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lbEnd attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:18]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lbBody attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.lbEnd attribute:NSLayoutAttributeBottom multiplier:1.0 constant:4]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lbBody attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:18]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgLeaveType attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgLeaveType attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:18]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.secondLineView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.msgLeaveType attribute:NSLayoutAttributeTop multiplier:1.0 constant:-3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.secondLineView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:1]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgProgress attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgProgress attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:18]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgType attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgType attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:21]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstLineView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.msgType attribute:NSLayoutAttributeBottom multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstLineView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:1]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgStartDate attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.firstLineView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgStartDate attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:18]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgEndDate attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.msgStartDate attribute:NSLayoutAttributeBottom multiplier:1.0 constant:4]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgEndDate attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:18]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgBody attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.msgEndDate attribute:NSLayoutAttributeBottom multiplier:1.0 constant:4]];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.secondLineView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.msgBody attribute:NSLayoutAttributeBottom multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.secondLineView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:1]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgLeaveTime attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.secondLineView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgLeaveTime attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:18]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgLeaveTime attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgType attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgType attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstLineView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstLineView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lbStart attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lbStart attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:40]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgStartDate attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.lbStart attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgStartDate attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lbEnd attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lbEnd attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:40]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgEndDate attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.lbEnd attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgEndDate attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lbBody attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lbBody attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:40]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgBody attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.lbBody attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgBody attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgLeaveType attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgLeaveType attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:40]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgLeaveTime attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.msgLeaveType attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgLeaveTime attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.msgProgress attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgProgress attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.secondLineView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.secondLineView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.LeaveBackView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
}

#pragma mark - public

- (void)setupLeaveBubbleView
{
    self.LeaveBackView = [[UIView alloc] init];
    self.LeaveBackView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.backgroundImageView addSubview:self.LeaveBackView];
    
    self.msgType = [[UILabel alloc] init];
    self.msgType.translatesAutoresizingMaskIntoConstraints = NO;
    [self.LeaveBackView addSubview:self.msgType];
    
    self.firstLineView = [[UIView alloc] init];
    self.firstLineView.translatesAutoresizingMaskIntoConstraints = NO;
    self.firstLineView.backgroundColor = RGB(240, 240, 240);
    [self.LeaveBackView addSubview:self.firstLineView];
    
    self.lbStart = [[UILabel alloc] init];
    self.lbStart.translatesAutoresizingMaskIntoConstraints = NO;
    self.lbStart.text = @"开始: ";
    self.lbStart.font = systemFont(14);
    self.lbStart.textColor = [UIColor lightGrayColor];
    [self.LeaveBackView addSubview:self.lbStart];
    
    self.msgStartDate = [[UILabel alloc] init];
    self.msgStartDate.translatesAutoresizingMaskIntoConstraints = NO;
    self.msgStartDate.font = systemFont(14);
    [self.LeaveBackView addSubview:self.msgStartDate];
    
    self.lbEnd = [[UILabel alloc] init];
    self.lbEnd.translatesAutoresizingMaskIntoConstraints = NO;
    self.lbEnd.text = @"结束: ";
    self.lbEnd.font = systemFont(14);
    self.lbEnd.textColor = [UIColor lightGrayColor];
    [self.LeaveBackView addSubview:self.lbEnd];
    
    self.msgEndDate = [[UILabel alloc] init];
    self.msgEndDate.translatesAutoresizingMaskIntoConstraints = NO;
    self.msgEndDate.font = systemFont(14);
    [self.LeaveBackView addSubview:self.msgEndDate];
    
    self.lbBody = [[UILabel alloc] init];
    self.lbBody.translatesAutoresizingMaskIntoConstraints = NO;
    self.lbBody.text = @"事由: ";
    self.lbBody.font = systemFont(14);
    self.lbBody.textColor = [UIColor lightGrayColor];
    [self.LeaveBackView addSubview:self.lbBody];
    
    self.msgBody = [[UILabel alloc] init];
    self.msgBody.translatesAutoresizingMaskIntoConstraints = NO;
    self.msgBody.font = systemFont(14);
    self.msgBody.numberOfLines = 2;
    [self.LeaveBackView addSubview:self.msgBody];
    
    self.secondLineView = [[UIView alloc] init];
    self.secondLineView.translatesAutoresizingMaskIntoConstraints = NO;
    self.secondLineView.backgroundColor = RGB(240, 240, 240);
    [self.LeaveBackView addSubview:self.secondLineView];
    
    self.msgLeaveType = [[UILabel alloc] init];
    self.msgLeaveType.translatesAutoresizingMaskIntoConstraints = NO;
    self.msgLeaveType.font = systemFont(14);
    self.msgLeaveType.textColor = [UIColor lightGrayColor];
    [self.LeaveBackView addSubview:self.msgLeaveType];
    
    self.msgLeaveTime = [[UILabel alloc] init];
    self.msgLeaveTime.translatesAutoresizingMaskIntoConstraints = NO;
    self.msgLeaveTime.font = systemFont(14);
    self.msgLeaveTime.textColor = HXSystemColor;
    [self.LeaveBackView addSubview:self.msgLeaveTime];
    
    self.msgProgress = [[UILabel alloc] init];
    self.msgProgress.textAlignment = NSTextAlignmentRight;
    self.msgProgress.translatesAutoresizingMaskIntoConstraints = NO;
    self.msgProgress.font = systemFont(14);
    self.msgProgress.textColor = [UIColor redColor];
    [self.LeaveBackView addSubview:self.msgProgress];

    
    [self _setupLeaveBubbleConstraints];
}

- (void)updateLeaveMargin:(UIEdgeInsets)margin
{
    if (_margin.top == margin.top && _margin.bottom == margin.bottom && _margin.left == margin.left && _margin.right == margin.right) {
        return;
    }
    _margin = margin;
    
    [self removeConstraints:self.marginConstraints];
    [self _setupLeaveBubbleMarginConstraints];
}

@end
